package es.kivitel.android.sms.kernel;

import java.util.ArrayList;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsEvent extends Event {
	public static final String TAG = SmsEvent.class.getName();

	public SmsEvent(){
		setComment("standard sms");
		setType(Event.SMS_EVENT);		
	}

	@Override
	public void setSubject(String subject) {
		return;
	}
	
	@Override
	public String getSubject() {
		return null; 
	}
	
	@Override
	public boolean send(Context ctx) {
		boolean result = false;
		switch(getMode()){ 
			case Event.BACKGROUND_MODE:
				result = sendBackgroundSMS(ctx);
				break;
			case Event.FOREGROUND_MODE:
				result = sendForegroundSMS(ctx);
				break;
		}
		return result;
	}
	
	private final boolean sendForegroundSMS(Context ctx){
		if(destination == null || content == null){
			Log.e(TAG,"Wrong input data unable to send message");
			return false;
		}		  
		Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:"+destination));  		
		sendIntent.putExtra("sms_body", content); 
		sendIntent.setType("vnd.android-dir/mms-sms");
		sendIntent.setData(Uri.parse("tel:"+destination));
		ctx.startActivity(sendIntent);   		
		return true;
	}
	
	private final boolean sendBackgroundSMS(Context ctx){
		  if(destination == null || content == null){
			  Log.e(TAG,"Wrong input data unable to send message");
			  return false;
		  }
		  ArrayList<String> parts;
		  ArrayList<PendingIntent> pis = new ArrayList<PendingIntent>();
		  PendingIntent pi;
		  boolean use7bitOnly=false;
		  int [] smsNumber;
		  if(ctx == null){
			  Log.e(TAG,"Unable to send message; context is not set");
			  return false;
		  }		  
		  pi = PendingIntent.getActivity(ctx, 0,new Intent(ctx, SmsEvent.class), 0);                
	      SmsManager sms = SmsManager.getDefault();
	      try{
	    	  smsNumber = SmsMessage.calculateLength(content,use7bitOnly);
	    	  //If more than one sending is required
	    	  if(smsNumber[0]>1){
	    		  parts = sms.divideMessage(content);
	    		  for(int i=0;i<smsNumber[0];i++)
	    			  pis.add(pi);
	    		  sms.sendMultipartTextMessage(destination, null, parts, pis, null);
	    	  }
	    	  else{
	    		  sms.sendTextMessage(destination, null, content, pi, null);	    		  
	    	  }
	      }catch(NullPointerException ex){
	    	  Log.e(TAG,"Sms content is too long; it's unable to send the message: "+ex.getMessage());
	    	  return false;
	      }
	      return true;
	  }
	
	/**
	 * @Override
	 *
	public boolean store(Context ctx) {
		boolean result = false;
		try {
			DatabaseManager.initialize(ctx);
			result = DatabaseManager.execute("INSERT INTO EVT_EVENT (EVT_COMMENT,EVT_START,EVT_CONTENT,EVT_DESTINATION) VALUES (\""+
					comment+"\","+sendingdate+",\""+content+"\",\""+destination+"\")");
		} catch (Exception e) {
			Log.e(TAG,"Could not insert the sms into DB: "+e.getMessage());
		}
		return result;
	}
	*/
}
